//
//  EditViewController.m
//  QuickCare
//
//  Created by lidabiao on 16/7/15.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "EditViewController.h"

#import "InfoEditHeaderView.h"

#import "InfoEditView.h"

@interface EditViewController ()

@property (nonatomic,weak) InfoEditHeaderView  *headerView;

@end

@implementation EditViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    InfoEditHeaderView *header = [InfoEditHeaderView infoEditHeaderView];
    
    header.userInfo = self.userInfo;
    
    __weak typeof(header) weakHeader = header;
    
    __weak typeof(self) weakSelf = self;
    
    [header setButtonBlock:^(EditStyle editStyle, NSString *title, NSString *text)
    {
        InfoEditView *editView = [[InfoEditView alloc] initWithFrame:self.navigationController.view.bounds userInfo:self.userInfo editStyle:editStyle title:title text:text];
        
        [editView setDoneBlock:^
        {
            weakHeader.userInfo = weakSelf.userInfo;
        }];
        
        [self.navigationController.view addSubview:editView];
    }];
    
    self.headerView = header;
    
    self.tableView.rowHeight = 44;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - tableViewDateSource/tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditViewControllerCell"];
    
    UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:1];
    
    UILabel *contentLabel = (UILabel *)[cell.contentView viewWithTag:2];
    
    if (indexPath.row == 0)
    {
        titleLabel.text = @"身高";
        
        if (self.userInfo.height.integerValue > 0)
        {
            contentLabel.text = [NSString stringWithFormat:@"%@ cm", self.userInfo.height];
        }else
        {
            contentLabel.text = @"未设置";
        }
    }
    if (indexPath.row == 1)
    {
        titleLabel.text = @"体重";
        
        if (self.userInfo.weight.integerValue > 0)
        {
            contentLabel.text = [NSString stringWithFormat:@"%@ kg", self.userInfo.weight];
        }else
        {
            contentLabel.text = @"未设置";
        }
    }
    if (indexPath.row == 2)
    {
        titleLabel.text = @"省份";
        
        if (self.userInfo.province)
        {
            contentLabel.text = self.userInfo.province;
        }else
        {
            contentLabel.text = @"未设置";
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditStyle editStyle;
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
    
    if (indexPath.row == 0)
    {
        editStyle = HeightStyle;
    }
    if (indexPath.row == 1)
    {
        editStyle = WeightStyle;
    }
    if (indexPath.row == 2)
    {
    editStyle = ProvinceStyle;
    }
    InfoEditView *editView = [[InfoEditView alloc] initWithFrame:self.navigationController.view.bounds userInfo:self.userInfo editStyle:editStyle title:label.text text:nil];
    
    __weak typeof(self) weakSelf = self;
    
    [editView setDoneBlock:^
    {
        [weakSelf.tableView reloadData];
    }];
    
    [self.navigationController.view addSubview: editView];
}

@end
